package com.bwie.goods.datastructureOne;

import cn.hutool.json.JSONArray;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class one {
    public static void main(String[] args) {
        // 创建对象方式
        Person person = new Person("张三", 25);
        Address address = new Address("上海", "奉贤", "10001");
        person.setAddress(address);
        person.addFriend(new Person("李四", 27, new Address("上海", "虹桥", "10002")));
        person.addFriend(new Person("王五", 24, new Address("杭州", "西湖", "20001")));
        String jsonString1 = JSON.toJSONString(person);
//        System.out.println(jsonString1);

        // 创建Map方式
        Map<String, Object> map = new HashMap<>();
        map.put("name", "张三");
        map.put("age", 25);
        Map<String, String> addressMap = new HashMap<>();
        addressMap.put("city", "上海");
        addressMap.put("state", "奉贤");
        addressMap.put("zip", "10001");
        map.put("address", addressMap);
        JSONArray friendsArray = new JSONArray();
        Map<String, Object> friend1Map = new HashMap<>();
        friend1Map.put("name", "李四");
        friend1Map.put("age", 27);
        Map<String, String> friend1AddressMap = new HashMap<>();
        friend1AddressMap.put("city", "上海");
        friend1AddressMap.put("state", "虹桥");
        friend1AddressMap.put("zip", "10002");
        friend1Map.put("address", friend1AddressMap);
        friendsArray.add(friend1Map);
        Map<String, Object> friend2Map = new HashMap<>();
        friend2Map.put("name", "王五");
        friend2Map.put("age", 24);
        Map<String, String> friend2AddressMap = new HashMap<>();
        friend2AddressMap.put("city", "杭州");
        friend2AddressMap.put("state", "西湖");
        friend2AddressMap.put("zip", "20001");
        friend2Map.put("address", friend2AddressMap);
        friendsArray.add(friend2Map);
        map.put("friends", friendsArray);
        String jsonString2 = JSON.toJSONString(map);
        try (OutputStreamWriter writer = new OutputStreamWriter(System.out, "UTF-8")) {
            writer.write(jsonString2);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 创建JSONObject方式
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "张三");
        jsonObject.put("age", 25);
        JSONObject addressJson = new JSONObject();
        addressJson.put("city", "上海");
        addressJson.put("state", "奉贤");
        addressJson.put("zip", "10001");
        jsonObject.put("address", addressJson);
        JSONArray friendsArray2 = new JSONArray();
        JSONObject friend1Json = new JSONObject();
        friend1Json.put("name", "李四");
        friend1Json.put("age", 27);
        JSONObject friend1AddressJson = new JSONObject();
        friend1AddressJson.put("city", "上海");
        friend1AddressJson.put("state", "虹桥");
        friend1AddressJson.put("zip", "10002");
        friend1Json.put("address", friend1AddressJson);
        friendsArray2.add(friend1Json);
        JSONObject friend2Json = new JSONObject();
        friend2Json.put("name", "王五");
        friend2Json.put("age", 24);
        JSONObject friend2AddressJson = new JSONObject();
        friend2AddressJson.put("city", "杭州");
        friend2AddressJson.put("state", "西湖");
        friend2AddressJson.put("zip", "20001");
        friend2Json.put("address", friend2AddressJson);
        friendsArray2.add(friend2Json);
        jsonObject.put("friends", friendsArray2);
        String jsonString3 = jsonObject.toJSONString();
        try (OutputStreamWriter writer = new OutputStreamWriter(System.out, "UTF-8")) {
            writer.write(jsonString3);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
class Person {
    private String name;
    private int age;
    private Address address;
    private List<Person> friends;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
        this.friends = new ArrayList<>();
    }

    public Person(String name, int age, Address address) {
        this(name, age);
        this.address = address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public void addFriend(Person friend) {
        this.friends.add(friend);
    }
}

class Address {
    private String city;
    private String state;
    private String zip;

    public Address(String city, String state, String zip) {
        this.city = city;
        this.state = state;
        this.zip = zip;
    }
}
